//
//  LCReadViewController.m
//  YinYueTai
//
//  Created by CongCong on 15-7-18.
//  Copyright (c) 2015年 CongCong. All rights reserved.
//

#import "LCReadViewController.h"
#import "CommonHeader.h"
@interface LCReadViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) LCReadModel *readModel;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageView;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation LCReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setDrawButton];
    [self creatCollection];
    [self registerNib];
    [self fetchWebDataWith:kReadUrlHeader body:kReadUrlBody];
}
- (void)registerNib
{
    self.flowLayout.sectionInset = UIEdgeInsetsMake(5, 2, 5, 2);
    self.flowLayout.minimumLineSpacing = 1;
    self.flowLayout.minimumInteritemSpacing = 1;
    [self.collectionView registerNib:[UINib nibWithNibName:@"LCReadLIstCell" bundle:nil] forCellWithReuseIdentifier:@"LCReadLIstCell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
}
- (void)initJosnModelWith:(NSDictionary *)dataDict
{
    NSDictionary *dict = dataDict[@"data"];
    self.readModel = [[LCReadModel alloc]initWithDictionary:dict error:nil];
    [self.collectionView reloadData];
}

#pragma mark - <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.readModel.list.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCReadLIstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCReadLIstCell" forIndexPath:indexPath];
    LCReadListModel *model = self.readModel.list[indexPath.row];
    [cell showModel:model];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((kScreenSize.width-8)/3, (kScreenSize.width-8)/3);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //点击事件
    LCReadListModel *model = self.readModel.list[indexPath.row];
    ContentViewController *content = [[ContentViewController alloc]init];
    content.type = model.type;
    content.title = model.name;
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:content];
    [self presentViewController:nav animated:YES completion:nil];
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *header;
    if([kind isEqual:UICollectionElementKindSectionHeader])
    {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height/662*200)];
        self.scrollView.contentSize = CGSizeMake(kScreenSize.width*self.readModel.carousel.count, kScreenSize.height/662*200);
        for (int i=0; i<self.readModel.carousel.count; i++)
        {
            LCReadcarouselMoedel *model = self.readModel.carousel[i];
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenSize.width*i, 0, kScreenSize.width, kScreenSize.height/662*200)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"hoder"]];
            
            [self.scrollView addSubview:imageView];
            
            
        }
        self.scrollView.pagingEnabled = YES;
        self.scrollView.bounces = NO;
        self.scrollView.delegate = self;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [self.scrollView addGestureRecognizer:tap];
        self.pageView = [[UIPageControl alloc]initWithFrame:CGRectMake(kScreenSize.width-kScreenSize.height/662*80, kScreenSize.height/662*180, kScreenSize.height/662*80, kScreenSize.height/662*20)];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenSize.height/662*180, kScreenSize.width, kScreenSize.height/662*20)];
        view.backgroundColor = [UIColor grayColor];
        view.alpha = 0.5;
        [header addSubview:view];
        self.pageView.numberOfPages = self.readModel.carousel.count;
        self.pageView.currentPage = 0;
        [header addSubview:self.scrollView];
        [header addSubview:self.pageView];
        
        if (!self.timer)
        {
            self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];
        }
        return header;

    }
    UICollectionReusableView *footer;
    if ([kind isEqual:UICollectionElementKindSectionFooter]) {
        footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Footer" forIndexPath:indexPath];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, 150)];
        image.image = [UIImage imageNamed:@"24read"];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(footTap:)];
        [image addGestureRecognizer:tap];
        
        [footer addSubview:image];
        return footer;
    }
    return nil;
}
- (void)footTap:(UIGestureRecognizer *)tap
{
    //24小时写作
    
}
- (void)tap
{
    //int i = _scrollView.contentOffset.x/kScreenSize.width;
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageView.currentPage = scrollView.contentOffset.x/kScreenSize.width;
}
- (void)changeTimeAtTimedisplay
{
    CGPoint p;
    if (_scrollView.contentOffset.x==kScreenSize.width*(self.readModel.carousel.count-1)) {
        p = CGPointMake(0, 0);
    }
    else
    {
        p = CGPointMake(_scrollView.contentOffset.x+kScreenSize.width, 0);
    }
    [UIView animateWithDuration:1 animations:^{
        _scrollView.contentOffset = p;
    }];
    
    int i = _scrollView.contentOffset.x/kScreenSize.width;
    self.pageView.currentPage = i;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(kScreenSize.width, 200);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(kScreenSize.width, 150);
}

@end
